<template>
  <dys-modal
    :open="open"
    :title="t('班级管理')"
    @cancel="emit('cancel')"
    @ok="handleSubmit"
    :confirm-loading="loading"
    :width="400"
  >
    <a-form :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
      <a-form-item :label="t('年级')" v-bind="validateInfos.grade">
        <select-value-set
          v-model:value="modelRef.grade"
          :placeholder="t('请选择年级')"
          value-set-code="GRADE"
          allow-clear
        ></select-value-set>
      </a-form-item>
      <a-form-item :label="t('班级')" v-bind="validateInfos.classNo">
        <a-input
          v-model:value="modelRef.classNo"
          :placeholder="t('请输入班级')"
          allow-clear
        ></a-input>
      </a-form-item>
      <a-form-item :label="t('班主任')" v-bind="validateInfos.classLeader">
        <popup-teacher v-model:value="modelRef.classLeader"></popup-teacher>
      </a-form-item>
      <a-form-item :label="t('班级状态')" v-bind="validateInfos.classStatus">
        <a-switch
          v-model:checked="modelRef.classStatus"
          checked-value="Y"
          un-checked-value="N"
          checked-children="在校"
          un-checked-children="毕业"
        ></a-switch>
      </a-form-item>
      <a-form-item :label="t('班级口号')">
        <a-textarea
          v-model:value="modelRef.classSlogan"
          :placeholder="t('请输入班级口号')"
          :rows="2"
          allow-clear
        ></a-textarea>
      </a-form-item>
    </a-form>
  </dys-modal>
</template>
<script lang="ts" setup>
import { pick } from "lodash";
import { useI18n } from "vue-i18n";
import { reactive, ref, watch } from "vue";
import { Form, message } from "ant-design-vue";
import { add, update } from "@/api/answer/base-data/class";
import type { FormRule } from "@/typing";
import type {
  ClassParams,
  QueryResult,
} from "@/api/answer/base-data/class/typing";
import PopupTeacher from "@/components/popup-teacher/index.vue";
import SelectValueSet from "@/components/select-value-set/index.vue";
interface Props {
  /**弹窗 */
  open: boolean;
  /**行数据 */
  model: QueryResult;
}
const props = defineProps<Props>();
const emit = defineEmits(["cancel", "ok"]);
const { t } = useI18n();
const initValue = (): ClassParams => ({
  classId: null,
  classNo: null,
  grade: null,
  classLeader: null,
  classSlogan: null,
  classStatus: "Y",
});
const modelRef = reactive(initValue());
const modelRule = reactive<FormRule<ClassParams>>({
  grade: [{ required: true, message: () => t("请输入年级") }],
  classNo: [{ required: true, message: () => t("请输入班级") }],
  classLeader: [{ required: true, message: () => t("请选择班主任") }],
});
const { validate, validateInfos, resetFields } = Form.useForm(
  modelRef,
  modelRule
);
watch([() => props.model, () => props.open], ([model]) => {
  resetFields(pick(model, Object.keys(modelRef)) || initValue());
});
const loading = ref(false);
/**提交 */
const handleSubmit = async () => {
  try {
    await validate();
  } catch {
    return message.warning(t("校验失败"));
  }
  loading.value = true;
  const { success } = await (props.model?.classId ? update : add)(modelRef);
  loading.value = false;
  if (success) {
    message.success(props.model?.classId ? t("修改成功") : t("新增成功"));
    emit("ok");
    emit("cancel");
  }
};
</script>
